function ajax(opts) {
    const defalutOpt = {
        url: '',
        type: 'get',
        data: {},
        dataType: 'string',
        async: true,
        success: function() {}
    }
    const option = {...defalutOpt, ...opts }
    const xhr = new XMLHttpRequest
    let str = ''
    for (let name in option.data) {
        str += `${name}=${option.data[name]}&`
    }
    str = str.slice(0, -1)
    if (option.type.toLowerCase() === 'get') {
        xhr.open(option.type, `${option.url}?${str}`, option.async)
        xhr.send()
    } else {
        xhr.open(option.type, option.url, option.async)
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
        xhr.send(str)
    }
    xhr.onload = function() {
        let res = xhr.responseText
        if (option.dataType.toLowerCase() === 'json') {
            // 将json字符串转换成Js对象
            res = JSON.parse(res)
        }
        option.success(res)
    }
}